package com.example.test.api;


import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import javax.crypto.*;
import javax.crypto.spec.SecretKeySpec;
import java.io.UnsupportedEncodingException;
import java.security.InvalidKeyException;
import java.security.NoSuchAlgorithmException;
import java.security.SecureRandom;

public class AES {
    private static final Logger LOGGER = LoggerFactory.getLogger(AES.class);

    public AES() {
    }

    public static String encryptAES(String content, String password) {
        byte[] encryptResult = encrypt(content, password);
        String encryptResultStr = parseByte2HexStr(encryptResult);
        encryptResultStr = ebotongEncrypto(encryptResultStr);
        return encryptResultStr;
    }

    public static String decrypt(String encryptResultStr, String password) {
        String decrpt = ebotongDecrypto(encryptResultStr);
        byte[] decryptFrom = parseHexStr2Byte(decrpt);
        byte[] decryptResult = decrypt(decryptFrom, password);
        return new String(decryptResult);
    }

    public static String ebotongEncrypto(String str) {
        String result = str;

        try {
            byte[] encodeByte = str.getBytes("UTF-8");
            result = Base64Code.encodeToString(encodeByte);
        } catch (Exception var3) {
            LOGGER.error("ebotongEncrypto", var3);
        }

        return result != null && result.length() != 0 ? result.replaceAll("\r\n", "").replaceAll("\r", "").replaceAll("\n", "") : "";
    }

    public static String ebotongDecrypto(String str) {
        try {
            byte[] encodeByte = Base64Code.decodeFromString(str);
            return encodeByte != null && encodeByte.length != 0 ? new String(encodeByte) : "";
        } catch (Exception var2) {
            LOGGER.error("ebotongDecrypto", var2);
            return str;
        }
    }

    private static byte[] encrypt(String content, String password) {
        try {
            KeyGenerator kgen = KeyGenerator.getInstance("AES");
            SecureRandom secureRandom = SecureRandom.getInstance("SHA1PRNG");
            secureRandom.setSeed(password.getBytes());
            kgen.init(128, secureRandom);
            SecretKey secretKey = kgen.generateKey();
            byte[] enCodeFormat = secretKey.getEncoded();
            SecretKeySpec key = new SecretKeySpec(enCodeFormat, "AES");
            Cipher cipher = Cipher.getInstance("AES");
            byte[] byteContent = content.getBytes("UTF-8");
            cipher.init(1, key);
            byte[] result = cipher.doFinal(byteContent);
            return result;
        } catch (NoSuchAlgorithmException var10) {
            LOGGER.error("encrypt", var10);
        } catch (NoSuchPaddingException var11) {
            LOGGER.error("encrypt", var11);
        } catch (InvalidKeyException var12) {
            LOGGER.error("encrypt", var12);
        } catch (UnsupportedEncodingException var13) {
            LOGGER.error("encrypt", var13);
        } catch (IllegalBlockSizeException var14) {
            LOGGER.error("encrypt", var14);
        } catch (BadPaddingException var15) {
            LOGGER.error("encrypt", var15);
        }

        return null;
    }

    private static byte[] decrypt(byte[] content, String password) {
        try {
            KeyGenerator kgen = KeyGenerator.getInstance("AES");
            SecureRandom secureRandom = SecureRandom.getInstance("SHA1PRNG");
            secureRandom.setSeed(password.getBytes());
            kgen.init(128, secureRandom);
            SecretKey secretKey = kgen.generateKey();
            byte[] enCodeFormat = secretKey.getEncoded();
            SecretKeySpec key = new SecretKeySpec(enCodeFormat, "AES");
            Cipher cipher = Cipher.getInstance("AES");
            cipher.init(2, key);
            byte[] result = cipher.doFinal(content);
            return result;
        } catch (NoSuchAlgorithmException var9) {
            LOGGER.error("encrypt", var9);
        } catch (NoSuchPaddingException var10) {
            LOGGER.error("encrypt", var10);
        } catch (InvalidKeyException var11) {
            LOGGER.error("encrypt", var11);
        } catch (IllegalBlockSizeException var12) {
            LOGGER.error("encrypt", var12);
        } catch (BadPaddingException var13) {
            LOGGER.error("encrypt", var13);
        }

        return null;
    }

    public static String parseByte2HexStr(byte[] buf) {
        StringBuffer sb = new StringBuffer();

        for (int i = 0; i < buf.length; ++i) {
            String hex = Integer.toHexString(buf[i] & 255);
            if (hex.length() == 1) {
                hex = '0' + hex;
            }

            sb.append(hex.toUpperCase());
        }

        return sb.toString();
    }

    public static byte[] parseHexStr2Byte(String hexStr) {
        if (hexStr.length() < 1) {
            return null;
        } else {
            byte[] result = new byte[hexStr.length() / 2];

            for (int i = 0; i < hexStr.length() / 2; ++i) {
                int high = Integer.parseInt(hexStr.substring(i * 2, i * 2 + 1), 16);
                int low = Integer.parseInt(hexStr.substring(i * 2 + 1, i * 2 + 2), 16);
                result[i] = (byte) (high * 16 + low);
            }

            return result;
        }
    }
}
